package com.testing.class5;

import java.sql.*;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @Classname MysqlTest
 * @Description 类型说明
 * @Date 2022/10/14 22:01
 * @Created by 特斯汀Roy
 */
public class MysqlTest {

    public static void main(String[] args) throws ClassNotFoundException, SQLException {
        Class.forName("com.mysql.cj.jdbc.Driver");
        Connection mycon = DriverManager.getConnection(
                "jdbc:mysql://47.105.110.138:3306/tpshop?useUnicode=true&useSSL=false&characterEncoding=utf-8",
                "Will",
                "willfqng"
        );
        //创建查询
        Statement statement = mycon.createStatement();
        //获取到一个结果集
        ResultSet resultSet = statement.executeQuery("select * from tp_goods where goods_name  like 'VIP13测试商品%'");
        //用LIst<Map<String,String>>来存储数据结果
        List<Map<String,String>> dbDataList=new ArrayList<>();
        //每一条数据，都是{字段=值}
        ResultSetMetaData headInfo = resultSet.getMetaData();
        //数据库的字段读取的时候从1开始计数

        //获取每一条数据,判断并且取下一行数据
        while(resultSet.next()){
            //每一条数据都是一个Map，遍历一行数据存储进去
            Map<String,String> onLineData=new HashMap<>();
            for (int i = 1; i <= headInfo.getColumnCount(); i++) {
                //System.out.println(headInfo.getColumnName(i)+"="+resultSet.getString(i));
                onLineData.put(headInfo.getColumnName(i),resultSet.getString(i));
            }
            //将这一行数据的map存到list里面
            dbDataList.add(onLineData);
//            System.out.println("---------------------------------");
        }
        System.out.println(dbDataList);

    }

}
